using System.Web.Services;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:		umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:		2006-03-08	
	Status:		release	
	Version:		2.0.0
	Build:		20070216
	License:		GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu EngineX.BlogX 
	/// This module is a component of the EngineX blogging and content management engine
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class userX : x_result {
		#region Private methods
		x_user userx;
		#endregion

		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		public userX() : base("blogX") {
			userx = new x_user(Application);
		}
		#endregion

		#region General web service methods
		/// <summary>Logs a user on to the system</summary>
		/// <remarks>NB: this is the only method that is not validated</remarks>
		/// <param name="username">login username</param>
		/// <param name="password">login password</param>
		[WebMethod] public XmlDocument Token(string username, string password) {
			try {
				userx.Login(username, password);
				_AddOk("token", userx.Token);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Adds a new user to the system</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		/// <param name="firstname">the user's first name</param>
		/// <param name="surname">the user's surname</param>
		/// <param name="email">the user's emaill address</param>
		/// <param name="telno">the user's contact number</param>
		/// <param name="secret">the user's secret password</param>
		/// <param name="password">the user's password</param>
		/// <param name="group">the user's group or profile</param>
		[WebMethod] public XmlDocument Add(string token, string name, string firstname, string surname, string email, string telno, string secret, string password, string group) {
			try {
				userx.Validate(token, "User.Register");
				userx.Add(name, firstname, surname, email, telno, secret, password, group);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Register a new user on the system</summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument Validate(string token) {
			try {
				userx.Validate(token);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Tests</summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument Test(string token) {
			try {
				_AddOk(userx.Users);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

		#region User manage web service methods
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		[WebMethod] public XmlDocument Get(string token, string name) {
			try {
				userx.Validate(token, "User.Get");
				userx.Get(name);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument List(string token) {
			try {
				userx.Validate(token, "User.List");
				_AddOk(userx.List().OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		[WebMethod] public XmlDocument Delete(string token, string name) {
			try {
				userx.Validate(token, "User.Delete");
				userx.Delete(name);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		/// <param name="firstname">the user's first name</param>
		/// <param name="surname">the user's surname</param>
		/// <param name="email">the user's emaill address</param>
		/// <param name="telno">the user's contact number</param>
		[WebMethod] public XmlDocument Edit(string token, string name, string firstname, string surname, string email, string telno) {
			try {
				userx.Validate(token, "User.Edit");
				userx.Update(name, firstname, surname, email, telno);
				_AddOk();
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		/// <param name="secret">the user's secret password</param>
		/// <param name="password">the user's password</param>
		[WebMethod] public XmlDocument ChangeLogin(string token, string name, string password, string secret) {
			try {
				userx.Validate(token, "User.ChangePassword");
				userx.ChangeLogin(name, password, secret);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);		
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		/// <param name="oldpass">the user's old password</param>
		/// <param name="newpass">the user's new password</param>
		/// <param name="newpass2">the user's new password repeated</param>
		[WebMethod] public XmlDocument ChangePassword(string token, string name, string oldpass, string newpass, string newpass2) {
			try {
				userx.Validate(token, "User.ChangePassword");
				if (newpass != newpass2)
					throw(new x_exception("error_confirm", "new password is not confirmed"));
				userx.ChangePassword(name, oldpass, newpass);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);		
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="user">the username of the user</param>
		/// <param name="group">the user's group or profile</param>
		/// <param name="isdefault">flag is blogspace is default for user (yes/no)</param>
		[WebMethod] public XmlDocument AddBlogspace(string token, string name, string user, string group, string isdefault) {
			try {
				userx.Validate(token, "User.AddBlogspace");
				bool isdef = (isdefault == "yes" || isdefault == "true");
				userx.AddBlogspace(name, user, group, isdef);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="html">the html of the blog</param>
		[WebMethod] public XmlDocument AddBlog(string token, string name, string title, string html) {
			try {
				userx.Validate(token, "User.Edit");
				name = (name == "" || name == null)? userx.Username : name;
				userx.AddBlog(name, userx.Name, title, html);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the username of the user</param>
		[WebMethod] public XmlDocument GetBlog(string token, string name) {
			try {
				userx.Validate(token, "User.Get");
				userx.GetBlog(name);
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

		#region Test web service methods
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument GetUserName(string token) {
			try {
				userx.Validate(token);
				string thisname = userx.Name;
				_AddOk(thisname);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

	}
}
